package a12;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

public class WordIndex {
	
	// ordered = all letters in word have been alphabetized. Is the key in hashmap for original word
	// scrambled = what the user enters

	private static Map<String, String> index = new HashMap<String, String>();

	public static void main(String[] args) {
		try {
			FileReader fileReader = new FileReader("10kwords.txt");
			WordIndex idx = new WordIndex(fileReader);
			String finalWord=" ";

			while (!"END".equals(finalWord)) {
				// Allow the user to enter a scrambled word
				String scrambledWord = JOptionPane
						.showInputDialog("Enter a scrambled word :");
				String word = lookup(scrambledWord);
				// and print the result. Keep going until they enter END.
				if(word==null){
					finalWord=
							JOptionPane.showInputDialog("I'm sorry, you're word could not be found.  \n Enter anything to continue, or \'END\' to exit");
				}
				else{
					finalWord=
							JOptionPane.showInputDialog(" Is your word here?   " + scrambledWord + " ==>" + word + "  \n Enter anything to continue, or \'END\' to exit");
				}
				if (finalWord == "END") {
					break;
				}
				//TODO add user's given word to index
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Finds words in the index that match a scrambled word.
	 * 
	 * @param scrambled
	 *            word
	 * @return matching words from the index
	 */
	static String lookup(String scrambled) {
		String ordered = sort(scrambled);	//orders scrambled word
		String word=index.get(ordered);		//gets the word(s?) from the key (ordered word)
		// TODO implement this
		return word;
	}

	/**
	 * Returns the letters of a String in alphabetized order
	 * 
	 * @param str
	 * @return the alphabetized String
	 */
	private static String sort(String str) {
		byte[] bytes = str.getBytes();
		Arrays.sort(bytes);
		return new String(bytes);
	}

	public WordIndex(InputStreamReader isr) {
		try {
			BufferedReader bufferedReader = new BufferedReader(isr);
			String word = bufferedReader.readLine();
			while (word != null) {
				addToIndex(word);
				word = bufferedReader.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Adds a word to the index. If the alphabetized word already exists, the
	 * word is appended to a comma-separated list of matching words. For
	 * example:
	 * 
	 * dgo => dog,god
	 * 
	 * @param word
	 */
	private void addToIndex(String word) {
		String orderedWord = sort(word);
		if(index.containsKey(orderedWord)){			// if word index already exists, add to the sting
			index.put(orderedWord, index.get(orderedWord) + " , " + word);
		}
		else
			index.put(orderedWord, word);	//ordered word, actual word


		// TODO implement this
	}

}
